import Vue from 'vue'
import VueRouter from 'vue-router'
// 路由懒加载
const Login = () => import(/* webpackChunkName: "login_home_welCome" */ '../components/Login')
const Home = () => import(/* webpackChunkName: "login_home_welCome" */ '../components/Home')
const Welcome = () => import(/* webpackChunkName: "ogin_home_welCome" */ '../components/Welcome')
const Users = () => import(/* webpackChunkName: "users_power" */ '../components/user/Users')
const Rights = () => import(/* webpackChunkName: "users_power" */ '../components/power/rights')
const Roles = () => import(/* webpackChunkName: "users_power" */ '../components/power/roles')
const Cate = () => import(/* webpackChunkName: "goods" */ '../components/goods/cate')
const Params = () => import(/* webpackChunkName: "goods" */ '../components/goods/params')
const goodsList = () => import(/* webpackChunkName: "goods" */ '../components/goods/list')
const Add = () => import(/* webpackChunkName: "goods" */ '../components/goods/add')
const Order = () => import(/* webpackChunkName: "order_report" */ '../components/order/order')
const Report = () => import(/* webpackChunkName: "order_report " */ '../components/report/report')

Vue.use(VueRouter)
const router = new VueRouter({
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        {
          path: '/welcome',
          component: Welcome
        },
        {
          path: '/users',
          component: Users
        },
        {
          path: '/rights',
          component: Rights
        },
        {
          path: '/roles',
          component: Roles
        },
        {
          path: '/categories',
          component: Cate
        },
        {
          path: '/params',
          component: Params
        },
        {
          path: '/goods',
          component: goodsList
        },
        {
          path: '/goods/add',
          component: Add
        },
        {
          path: '/orders',
          component: Order
        },
        {
          path: '/reports',
          component: Report
        }
      ]
    }
  ]
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from代表冲哪个路径过来
  // next() 是一个函数, 表示放行,强制跳转next('/login')
  if (to.path === '/login') {
    return next()
  }
  // 获取token
  const haveToken = window.sessionStorage.getItem('token')
  if (!haveToken) {
    return next('/login')
  }
  next()
})
export default router
